/* SharePoint Console Project
 * Http://SharePointConsole.codeplex.com
 * Copyright Jean-Alain Baeyens (2009)
 * Software under Microsoft Reciprocal License (Ms-RL)
 */

using System;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

using Sharepoint.Console;
using Sharepoint.Console.Exceptions;

namespace Sharepoint.Console.Commands
{
    public class Disconnect : BaseCommande
    {
        public override string ShortHelp
        {
            get
            {
                return "Close the openned connection";
            }
        }

        public override string LongHelp
        {
            get
            {
                return "Close the connection previously openned by Connect statement.";
            }
        }

        public override string Syntax
        {
            get
            {
                return "Syntax: Disconnect";
            }
        }
        public override string Execute(string[] args)
        {
            if (args != null && args.Length > 1) throw new SyntaxEception("Invalid command argument", Syntax);
            
            if (this.SharepointConnection.Web == null && this.SharepointConnection.Site == null)
            {
                return "Already disconected";
            }
            
            else
            {
                Close();
                return "Disconected";
            }
        }

        public void Close()
        {
            if (this.SharepointConnection.Web != null)
            {
                this.SharepointConnection.Web.Dispose();
                this.SharepointConnection.Web = null;
            }
            if (this.SharepointConnection.Site != null)
            {
                this.SharepointConnection.Site.Dispose();
                this.SharepointConnection.Site = null;
            }
        }
    }
}
